//
//  FavouriteEmotionDao.swift
//  Tinsecret_TW
//
//  Created by wanghui on 2019/2/19.
//  Copyright © 2019年 tst. All rights reserved.
//

import Foundation
import WCDBSwift

class FavouriteEmotionDao {
    // 获取所有收藏的表情
    static func getAllEmotions(groupId:Int64) throws -> [FavouriteEmotion] {
        var returnData = [FavouriteEmotion]()
        let db = SQLiteHelper.getDb()
        let emotions:[FavouriteEmotion] = try db.getObjects(on: FavouriteEmotion.CodingKeys.all, fromTable: FavouriteEmotion.table)
        returnData = emotions
        return returnData
    }
    
    //根据id 获取FavouriteEmotion
    static func getEmotion(id:Int64) throws -> FavouriteEmotion? {
        var returnData:FavouriteEmotion?
        let db = SQLiteHelper.getDb()
        let emotion:FavouriteEmotion? = try db.getObject(on: FavouriteEmotion.CodingKeys.all, fromTable: FavouriteEmotion.table, where: FavouriteEmotion.CodingKeys.id == id)
        returnData = emotion
        return returnData
    }
    
    //根据url 获取FavouriteEmotion
    static func getEmotion(url:String) throws -> FavouriteEmotion? {
        var returnData:FavouriteEmotion?
        let db = SQLiteHelper.getDb()
        let emotion:FavouriteEmotion? = try db.getObject(on: FavouriteEmotion.CodingKeys.all, fromTable: FavouriteEmotion.table, where:FavouriteEmotion.CodingKeys.url == url)
        returnData = emotion
        return returnData
    }
    
    static func save(favEmotion:FavouriteEmotion) throws {
        let db = SQLiteHelper.getDb()
        try db.insert(favEmotion, intoTable: FavouriteEmotion.table)
    }
    
    //根据url 删除收藏的表情
    static func delete(url:String) throws {
        let db = SQLiteHelper.getDb()
        try db.delete(fromTable: FavouriteEmotion.table, where: FavouriteEmotion.CodingKeys.url == url)
    }
}
